package com.nsl.my_youlai.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nsl.my_youlai.common.constant.SystemConstants;
import com.nsl.my_youlai.common.model.ElSelectOption;
import com.nsl.my_youlai.common.utils.SecurityUtils;
import com.nsl.my_youlai.convert.RoleConverter;
import com.nsl.my_youlai.mapper.SysRoleMapper;

import com.nsl.my_youlai.model.entity.SysRole;
import com.nsl.my_youlai.service.SysRoleService;
import lombok.RequiredArgsConstructor;

import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;


/**
 * 角色业务实现类
 *
 * @author haoxr
 * @since 2022/6/3
 */
@Service
@RequiredArgsConstructor
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    private  final RoleConverter roleConverter;

    /**
     * 获取最大范围的数据权限
     *
     * @param roles 角色编码集合
     * @return {@link Integer} – 数据权限范围
     */
    @Override
    public Integer getMaximumDataScope(Set<String> roles) {
        Integer dataScope = this.baseMapper.getMaximumDataScope(roles);
        return dataScope;
    }

    @Override
    public List<ElSelectOption> listRoleOptions() {
        // 查询数据
        List<SysRole> roleList = this.list(new LambdaQueryWrapper<SysRole>()
                .ne(!SecurityUtils.isRoot(), SysRole::getCode, SystemConstants.ROOT_ROLE_CODE)
                .select(SysRole::getId, SysRole::getName)
                .orderByAsc(SysRole::getSort)
        );

        // 实体转换

        return roleConverter.entities2Options(roleList);
    }

}
